$(function(){
   
    var register = new Register();
    register.init().init();
    
});

function Register(){
    this.init = function(){
        return REGISTER;
    };
    
    var REGISTER = {
        init : function(){
            this.listeners.bindEvents();
            
            REGISTER.funs.defaultUnChecked();
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindToggleSubmitBtn : function(){
                $("#id_agreement").click(function(){
                    var type = $(this).attr("checked");
                    if('checked' == type){
                        $("#btn_submit").removeClass("rectangle-small-button-disable");
                    }else{
                        $("#btn_submit").addClass("rectangle-small-button-disable");
                    }
                });
            },
            bindRegister : function(){
                $("#btn_submit").click(function(){
                    var checked = $("#id_agreement").attr("checked");
                    if(!checked){
                        return false;
                    }
                    
                    var $form = $("#register-form");
                    
                    $form.validate({
                        rules : {
                            email : {
                                required : true,
                                email : true,
                                remote : {
                                    url :  SYSTEM_DATA.contextPath + "/User/isHas.shtml",
                                    type : "post",
                                    data : {
                                        email : function() {
                                            return $.trim($("#id_email").val());
                                        }
                                    }
                                }
                            },
                            nick_name : {
                                required : true
                            },
                            password : {
                                required : true,
                                minlength:6,
                                maxlength:16
                            },
                            password2:{
                                equalTo : "#id_password2"
                            },
                            check_code:{
                                required : true
                            }
                        },
                        messages : {
                            email : {
                                remote : "邮箱已注册"
                            }
                        },
                        onsubmit : true
                    });
                    $form.submit();
                });
            }
        },
        funs : {
            defaultUnChecked : function(){
                $("#id_agreement").removeAttr("checked");
            }
        }
    };
}




